<template>
  <div class="home">
    <div class="search">
      <el-input class="search_input" placeholder="请输入名称" v-model="params.name"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="load">搜索</el-button>
      <el-button type="warning" icon="el-icon-search" @click="clearSearch">清空</el-button>
    </div>

    <el-table :data="tableData" row-key="id" default-expand-all>
      <el-table-column prop="id" label="编号">
      </el-table-column>
      <el-table-column prop="name" label="名称">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间">
      </el-table-column>
      <el-table-column prop="updatetime" label="更新时间">
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template v-slot="scope">
          <el-button style="margin-right:10px" size="mini" v-if="!scope.row.pid" type="success" @click="handleAdd(scope.row.id)">
            添加二级分类</el-button>
          <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
            <el-form :inline="true" :model="form" :rules="rules" ref="form" class="form" label-width="120px">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称"></el-input>
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="save">确 定</el-button>
            </span>
          </el-dialog>

          <el-button size="mini" type="primary" @click="$router.push('/layout/edit-category?id=' + scope.row.id)">修改
          </el-button>

          <template>
            <el-popconfirm style="margin-left:10px" title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button size="mini" type="danger" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </template> 
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination :page-size="params.pageSize" :current-page="params.pageNum" @current-change="changeCurrentPage"
        background layout="prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Category',
  data() {
    return {
      form: {},
      tableData: [],
      total: 0,
      params: {
        name: '',
        pageNum: 1,
        pageSize: 5
      },
      dialogVisible: false,
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/api/category/page", { params: this.params }).then(res => {
        if (res.code === '200') {
          console.log(res.data);
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      })
    },
    changeCurrentPage(index) {
      this.params.pageNum = index;
      this.load();
    },
    del(id) {
      this.request.delete("/api/category/delete/" + id).then(res => {
        console.log(res);
        if (res.code === '200') {
          this.$notify.success("删除成功");
          this.params.pageNum = 1;
          this.load();
        }
      })
    },
    clearSearch() {
      this.params = {};
    },
    handleAdd(id) {
      this.form.pid = id;
      this.dialogVisible = true;
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.request.post("/api/category/save", this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success("添加成功");
            } else {
              this.$notify.error(res.msg);
            }
            this.form = {};
            this.dialogVisible = false;
            this.load();
          })
        } else {
          console.log('error submit!!');
          this.dialogVisible = false
          return false;
        }
      });
    }
  }
}
</script>


<style lang="scss" scoped>
.home {
  .search {
    display: flex;
    margin: 10px 0;

    .search_input {
      width: 20%;
      margin-right: 10px;
    }
  }

  .page {
    display: flex;
    justify-content: center;
    margin: 20px 0;
  }
}
</style>